package com.lfd.springasync.controller;

/**
 * @项目名： spring-emq
 * @包名： com.lfd.springasync.controller
 * @文件名: AsyncController
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/13 21:22
 * @描述：
 */
import com.lfd.springasync.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;
@Slf4j
@RestController
public class AsyncController {

    @Autowired
    private AsyncService asyncService;

    @GetMapping("/testAsync")
    public String testAsync() {
        log.info("AsyncController调用异步方法开始"+Thread.currentThread().getName());
        asyncService.asyncMethod();  // 调用异步方法
        log.info("AsyncController调用异步方法结束"+Thread.currentThread().getName());
        return "异步调用成功";
    }
    @GetMapping("/testAsyncReturn")
    public String testAsyncReturn() throws Exception {
        log.info("AsyncController testAsyncReturn 调用异步方法开始");
        Future<String> stringFuture = asyncService.asyncMethodWithReturn();
        log.info("AsyncController testAsyncReturn 异步方法已调用，等待结果...");
        String string = stringFuture.get();
        log.info("AsyncController testAsyncReturn 异步方法结果...{}",string);
        return string;
    }

    @GetMapping("/testAsyncMethodExecutor")
    public String asyncMethodExecutor() {
        log.info("AsyncController 调用异步方法开始"+Thread.currentThread().getName());
        asyncService.asyncMethodExecutor();  // 调用异步方法
        log.info("AsyncController 调用异步方法结束"+Thread.currentThread().getName());
        return "异步调用成功";
    }
    @GetMapping("/testAsyncMethodExecutor2")
    public String asyncMethodExecutor2() {
        log.info("AsyncController 调用异步方法开始2"+Thread.currentThread().getName());
        asyncService.asyncMethodExecutor2();  // 调用异步方法
        log.info("AsyncController 调用异步方法结束2"+Thread.currentThread().getName());
        return "异步调用成功";
    }
}
